//
//  XLCommitGender.m
//  XiangLove
//
//  Created by 宋振 on 2018/8/23.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLCommitGender.h"
#import "XLUserInfoModel.h"

#define kSelectColor    RGB(255, 187, 1)
#define kUnSelectColor  RGB(204, 204, 204)

@interface GenderView : UIView

//图片设置 (性别)
@property (nonatomic, strong) UIImageView *  genderImgView;

//图片设置 (是否选择)
@property (nonatomic, strong) UIImageView *  selectImgView;

//标题显示
@property (nonatomic, strong) UILabel *  genderLbl;

//是否被选择
@property (nonatomic, assign) BOOL  isSelect;

//标题
@property (nonatomic, copy) NSString *  genderTitle;

/**
 点击事件
 */
- (void)addTapTarget:(id)sel action:(SEL)action;

@end

@implementation GenderView

/**
 初始化
 */
- (instancetype)init
{
    self = [super init];
    
    if (self) {
        
        //初始化变量
        [self initTools];
        
        //绘制
        [self customeView];
    }
    
    return self;
}

/**
 初始化变量
 */
- (void)initTools
{
    self.isSelect = NO;
    self.layer.borderColor = kUnSelectColor.CGColor;
}

/**
 添加手势事件
 */
- (void)addTapTarget:(id)sel action:(SEL)action
{
    UITapGestureRecognizer *tapGuesture = [[UITapGestureRecognizer alloc] initWithTarget:sel action:action];
    tapGuesture.numberOfTapsRequired = 1;
    tapGuesture.numberOfTouchesRequired = 1;
    
    [self addGestureRecognizer:tapGuesture];
}

/**
 绘制
 */
- (void)customeView
{
    //图标
    self.genderImgView = [[UIImageView alloc] init];
    self.genderImgView.tintColor = kUnSelectColor;
    [self addSubview:self.genderImgView];
    
    [self.genderImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(11);
        make.centerX.equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
    
    //标题
    self.genderLbl = [[UILabel alloc] init];
    self.genderLbl.textAlignment = NSTextAlignmentCenter;
    self.genderLbl.textColor = kUnSelectColor;
    [self addSubview:self.genderLbl];
    
    [self.genderLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.genderImgView.mas_bottom).offset(10);
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.equalTo(self.mas_bottom).offset(-10);
    }];
    
    //选择图片
    self.selectImgView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"popup_icon_check_n"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]];
    self.selectImgView.hidden = YES;
    [self addSubview:self.selectImgView];
    
    [self.selectImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(30, 28));
    }];
}

/**
 响应点击
 */
- (void)tapSelectAction:(UITapGestureRecognizer *)sender
{
    self.isSelect = !self.isSelect;
}

- (void)setIsSelect:(BOOL)isSelect
{
    _isSelect = isSelect;
    
    if (isSelect) {
        self.genderImgView.tintColor = kSelectColor;
        self.genderLbl.textColor = kSelectColor;
        self.selectImgView.hidden = NO;
        self.layer.borderColor = kSelectColor.CGColor;
    }else{
        self.genderImgView.tintColor = kUnSelectColor;
        self.genderLbl.textColor = kUnSelectColor;
        self.selectImgView.hidden = YES;
        self.layer.borderColor = kUnSelectColor.CGColor;
    }
}

@end



@interface XLCommitGender ()

//男
@property (nonatomic, strong) GenderView *  genderGirl;

//女
@property (nonatomic, strong) GenderView *  genderSir;

@end

@implementation XLCommitGender
{
    NSInteger _index;
}

/**
 初始化
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = RGBA(255, 255, 255, 1);
        self.layer.cornerRadius = 12;
        self.layer.masksToBounds = YES;
        
        //设置默认选择男 0男 1女
        _index = 0;
        
        /**
         显示UI
         */
        [self customeView];
    }
    return self;
}

/**
 显示UI
 */
- (void)customeView
{
    /**
     提示
     */
    UILabel *titleLbl = [[UILabel alloc] init];
    
    titleLbl.attributedText = [[NSAttributedString alloc] initWithString:@"选择您的性别" attributes:@{
                                                                                                 NSKernAttributeName:@1,
                                                                                                 NSForegroundColorAttributeName:RGB(17, 17, 17),
                                                                                                 NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:17]
                                                                                                 }];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:titleLbl];
    
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(11);
        make.centerX.equalTo(self.mas_centerX);
        make.height.mas_equalTo(24);
    }];
    
    /**
     帅哥
     */
    self.genderSir = [[GenderView alloc] init];
    self.genderSir.layer.borderWidth = 1;
    self.genderSir.layer.cornerRadius = 3;
    self.genderSir.layer.masksToBounds = YES;
    self.genderSir.isSelect = YES;
    self.genderSir.genderImgView.image = [[UIImage imageNamed:@"login_icon_nan_n"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
    self.genderSir.genderLbl.text = @"我是帅哥";
    [self.genderSir addTapTarget:self action:@selector(tapSelectGenderAction:)];
    [self addSubview:self.genderSir];
    
    [self.genderSir mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLbl.mas_top).offset(27);
        make.left.equalTo(self.mas_left).offset(15);
        make.size.mas_equalTo(CGSizeMake(108, 84));
    }];

    
    
    /**
     美女
     */
    self.genderGirl = [[GenderView alloc] init];
    self.genderGirl.layer.borderWidth = 1;
    self.genderGirl.layer.cornerRadius = 3;
    self.genderGirl.layer.masksToBounds = YES;
    self.genderGirl.isSelect = NO;
    self.genderGirl.genderImgView.image = [[UIImage imageNamed:@"login_icon_nv_s"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)];
    self.genderGirl.genderLbl.text = @"我是美女";
    [self.genderGirl addTapTarget:self action:@selector(tapSelectGenderAction:)];
    [self addSubview:self.genderGirl];
    
    [self.genderGirl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.genderSir.mas_top);
        make.width.height.equalTo(self.genderSir);
        make.right.equalTo(self.mas_right).offset(-15);
    }];
    
    //完成按钮
    self.saveSucessBt = [[UIButton alloc] init];
    
    self.saveSucessBt.layer.cornerRadius = 5;
    self.saveSucessBt.layer.masksToBounds = YES;
    [self.saveSucessBt setTitle:@"完成" forState:(UIControlStateNormal)];
    [self.saveSucessBt setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.saveSucessBt.backgroundColor = RGB(255, 187, 1);
    [self.saveSucessBt addTarget:self action:@selector(saveSucessAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.saveSucessBt];
    
    [self.saveSucessBt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(240, 40));
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.equalTo(self.mas_bottom).offset(-39);
    }];
    
    //提醒
    UILabel *propertLbl = [[UILabel alloc] init];
    [self addSubview:propertLbl];
    
    propertLbl.textAlignment = NSTextAlignmentCenter;
    
    propertLbl.attributedText = [[NSAttributedString alloc] initWithString:@"性别一旦设置后不可更改" attributes:@{
                                                                                                       NSKernAttributeName:@1,
                                                                                                       NSFontAttributeName:[UIFont systemFontOfSize:12],
                                                                                                       NSForegroundColorAttributeName:RGB(153, 153, 153)
                                                                                                       }];
    
    [propertLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(17);
        make.bottom.equalTo(self.mas_bottom).offset(-14);
    }];
    
    
}

/**
 点击选择事件
 */
- (void)tapSelectGenderAction:(UITapGestureRecognizer *)tapGesture
{
    if (tapGesture.view == self.genderSir) {
        
        //选择男
        _index = 0;
        self.genderSir.isSelect = YES;
        self.genderGirl.isSelect = NO;
    }else if (tapGesture.view == self.genderGirl){
        //选择女
        _index = 1;
        self.genderSir.isSelect = NO;
        self.genderGirl.isSelect = YES;
    }else{
        NSLog(@"未选中");
    }
}

//完成设置
- (void)saveSucessAction:(UIButton *)sender
{
    NSLog(@"点击完成性别设置！");
    
    //设置性别
    [[[XLHTTPSessionManager sharedManager] XLPOST:@"/api/v2/login/EditUserSex" parameters:@{@"Id":[XLAccountManager shareInstance].model.Id,@"Sex":@(_index)} resultClass:nil] subscribeNext:^(id x) {
        
        //获取有缓存的信息
        XLUserInfoModel *infoModel = [XLAccountManager shareInstance].model;
        infoModel.Status = @"0";
        
        //更新信息
        [[XLAccountManager shareInstance] updateUserInfo:infoModel];
        
        if (self.delegate && [_delegate respondsToSelector:@selector(commitGenderSelectIndex:)]) {
            [self.delegate commitGenderSelectIndex:_index];
        }
        
    } error:^(NSError *error) {
        
        [HelpTool startTipViewWithText:@"修改失败" duration:1 type:(kTipViewTypeNone)];
        
    }];
    
    
}


@end
